package com.taobao.txc.parser.b.b;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.taobao.txc.common.LoggerInit;
import com.taobao.txc.common.LoggerWrap;
import com.taobao.txc.common.TxcContext;
import com.taobao.txc.common.config.o;
import com.taobao.txc.parser.b.a.d;
import com.taobao.txc.parser.struct.f;
import com.taobao.txc.parser.struct.k;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;

public class a
{
  private static final LoggerWrap a = LoggerInit.logger;
  private static long b = 100000L;
  private static long c = 900000L;
  private static final Cache<String, k> d = CacheBuilder.newBuilder().maximumSize(b).expireAfterWrite(c, TimeUnit.MILLISECONDS).softValues().build();
  
  public static k a(com.taobao.txc.parser.b.a.g paramg, com.taobao.txc.resourcemanager.b.a.a parama)
  {
    k localk = null;
    String str1 = paramg.j().b();
    String str2 = paramg.j().c();
    localk = a(parama, str1);
    localk.d(str2);
    return localk;
  }
  
  public static k a(com.taobao.txc.resourcemanager.b.a.a parama, String paramString)
  {
    if (StringUtils.isEmpty(paramString)) {
      throw new com.taobao.txc.common.b.c("TableMeta cannot fetched without tableName");
    }
    if (parama == null) {
      throw new com.taobao.txc.common.b.c("TableMeta cannot fetched without Connection");
    }
    String str1 = null;
    if ((parama instanceof com.taobao.txc.resourcemanager.b.a.a)) {
      try
      {
        str1 = parama.a().d();
      }
      catch (Exception localException1)
      {
        str1 = null;
      }
    }
    if ((StringUtils.isEmpty(str1)) || ("null".compareToIgnoreCase(str1) == 0)) {
      try
      {
        str1 = parama.getMetaData().getURL();
      }
      catch (Exception localException2) {}
    }
    if (StringUtils.isEmpty(str1)) {
      str1 = "NULL";
    }
    int i = 0;
    k localk1 = null;
    String str2 = str1 + "." + paramString;
    List localList = o.a().h(com.taobao.txc.a.b.g.f());
    if (localList != null)
    {
      localObject = localList.iterator();
      while (((Iterator)localObject).hasNext())
      {
        Pattern localPattern = (Pattern)((Iterator)localObject).next();
        if ((localPattern.matcher(paramString).matches()) || (localPattern.matcher(paramString.toLowerCase()).matches()))
        {
          str2 = localPattern.pattern();
          i = 1;
          break;
        }
      }
    }
    Object localObject = str2;
    try
    {
      localk1 = (k)d.get(localObject, new b(paramString, (String)localObject, parama));
    }
    catch (ExecutionException localExecutionException)
    {
      a.a(com.taobao.txc.common.b.b.Q.bd, "tableMeta cache error: %s", new Object[] { localExecutionException.getMessage() });
    }
    if (localk1 == null) {
      try
      {
        a.info("meta is null, fetch schema of " + paramString);
        localk1 = c(parama, paramString);
      }
      catch (SQLException localSQLException)
      {
        a.a(com.taobao.txc.common.b.b.F.bd, "tableMeta error: %s", new Object[] { localSQLException.getMessage() });
      }
    }
    if (localk1 == null) {
      throw new com.taobao.txc.common.b.c(String.format("[xid:%s]get tablemeta failed", new Object[] { TxcContext.getCurrentXid() }));
    }
    if (i != 0)
    {
      k localk2 = new k(localk1);
      localk2.b(paramString);
      localk2.a(str1);
      return localk2;
    }
    return localk1;
  }
  
  private static k c(com.taobao.txc.resourcemanager.b.a.a parama, String paramString)
  {
    Statement localStatement = null;
    ResultSet localResultSet1 = null;
    ResultSet localResultSet2 = null;
    try
    {
      localStatement = parama.b().createStatement();
      StringBuffer localStringBuffer = new StringBuffer("select * from ");
      if ((com.taobao.txc.resourcemanager.c.e != null) && (com.taobao.txc.resourcemanager.c.e.contains(paramString.toUpperCase()))) {
        localStringBuffer.append("`").append(paramString).append("`");
      } else {
        localStringBuffer.append(paramString);
      }
      localStringBuffer.append(" limit 1");
      localResultSet1 = localStatement.executeQuery(localStringBuffer.toString());
      localObject1 = localResultSet1.getMetaData();
      DatabaseMetaData localDatabaseMetaData = parama.b().getMetaData();
      k localk2 = a((ResultSetMetaData)localObject1, localDatabaseMetaData);
      return localk2;
    }
    catch (Exception localException)
    {
      Object localObject1;
      if ((localException instanceof SQLException))
      {
        if ("42000".equals(((SQLException)localException).getSQLState())) {
          try
          {
            localResultSet2 = localStatement.executeQuery("select COLUMN_NAME,DATA_TYPE,DATA_LENGTH from user_tab_cols where table_name='" + paramString + "'");
            localObject1 = a(localResultSet2, parama, paramString);
            return localObject1;
          }
          catch (SQLException localSQLException)
          {
            a.warn("failed to handle 42000 case: %s", localSQLException.getMessage());
          }
          finally
          {
            if (localResultSet2 != null) {
              localResultSet2.close();
            }
          }
        }
        throw ((SQLException)localException);
      }
      a.a(com.taobao.txc.common.b.b.D.bd, "schema of " + paramString + " cannot be fetched", localException);
      k localk1 = null;
      return localk1;
    }
    finally
    {
      if (localResultSet1 != null) {
        localResultSet1.close();
      }
      if (localStatement != null) {
        localStatement.close();
      }
    }
  }
  
  private static k a(ResultSet paramResultSet, com.taobao.txc.resourcemanager.b.a.a parama, String paramString)
  {
    String str1 = paramString;
    k localk = new k();
    localk.b(str1);
    while (paramResultSet.next())
    {
      localObject1 = new f();
      ((f)localObject1).c(str1);
      ((f)localObject1).d(paramResultSet.getString("COLUMN_NAME").toUpperCase());
      localObject2 = paramResultSet.getString("DATA_TYPE");
      if (StringUtils.equalsIgnoreCase((String)localObject2, "NUMBER")) {
        ((f)localObject1).a(-5);
      } else if (StringUtils.equalsIgnoreCase((String)localObject2, "VARCHAR2")) {
        ((f)localObject1).a(12);
      }
      ((f)localObject1).b(paramResultSet.getInt("DATA_LENGTH"));
      localk.c().put(((f)localObject1).b(), localObject1);
    }
    Object localObject1 = null;
    Object localObject2 = null;
    try
    {
      localObject1 = parama.b().createStatement();
      localObject2 = ((Statement)localObject1).executeQuery("select a.constraint_name,  a.column_name from user_cons_columns a, user_constraints b  where a.constraint_name = b.constraint_name and b.constraint_type = 'P' and a.table_name ='" + str1 + "'");
      while (((ResultSet)localObject2).next())
      {
        String str2 = ((ResultSet)localObject2).getString(1);
        String str3 = ((ResultSet)localObject2).getString(2).toUpperCase();
        f localf = (f)localk.c().get(str3);
        com.taobao.txc.parser.struct.g localg;
        if (localk.d().containsKey(str2))
        {
          localg = (com.taobao.txc.parser.struct.g)localk.d().get(str2);
          localg.a().add(localf);
        }
        else
        {
          localg = new com.taobao.txc.parser.struct.g();
          localg.b(str2);
          localg.a().add(localf);
          localg.a(com.taobao.txc.parser.struct.a.a);
          localk.d().put(str2, localg);
        }
      }
    }
    finally
    {
      if (localObject2 != null) {
        ((ResultSet)localObject2).close();
      }
      if (localObject1 != null) {
        ((Statement)localObject1).close();
      }
    }
    return localk;
  }
  
  private static k a(ResultSetMetaData paramResultSetMetaData, DatabaseMetaData paramDatabaseMetaData)
  {
    String str1 = paramResultSetMetaData.getTableName(1);
    String str2 = paramResultSetMetaData.getSchemaName(1);
    String str3 = paramResultSetMetaData.getCatalogName(1);
    if ((com.taobao.txc.resourcemanager.c.e != null) && (com.taobao.txc.resourcemanager.c.e.contains(str1.toUpperCase()))) {
      str1 = "`" + str1 + "`";
    }
    k localk = new k();
    localk.b(str1);
    if ((str2 == null) || (str2.isEmpty())) {
      localk.a(str3);
    } else {
      localk.a(str2);
    }
    ResultSet localResultSet = paramDatabaseMetaData.getColumns(str3, str2, str1, "%");
    while (localResultSet.next())
    {
      localObject1 = new f();
      ((f)localObject1).a(localResultSet.getString("TABLE_CAT"));
      ((f)localObject1).b(localResultSet.getString("TABLE_SCHEM"));
      ((f)localObject1).c(localResultSet.getString("TABLE_NAME"));
      ((f)localObject1).d(localResultSet.getString("COLUMN_NAME").toUpperCase());
      ((f)localObject1).a(localResultSet.getInt("DATA_TYPE"));
      ((f)localObject1).e(localResultSet.getString("TYPE_NAME"));
      ((f)localObject1).b(localResultSet.getInt("COLUMN_SIZE"));
      ((f)localObject1).c(localResultSet.getInt("DECIMAL_DIGITS"));
      ((f)localObject1).d(localResultSet.getInt("NUM_PREC_RADIX"));
      ((f)localObject1).e(localResultSet.getInt("NULLABLE"));
      ((f)localObject1).f(localResultSet.getString("REMARKS"));
      ((f)localObject1).g(localResultSet.getString("COLUMN_DEF"));
      ((f)localObject1).f(localResultSet.getInt("SQL_DATA_TYPE"));
      ((f)localObject1).g(localResultSet.getInt("SQL_DATETIME_SUB"));
      ((f)localObject1).h(localResultSet.getInt("CHAR_OCTET_LENGTH"));
      ((f)localObject1).i(localResultSet.getInt("ORDINAL_POSITION"));
      ((f)localObject1).h(localResultSet.getString("IS_NULLABLE"));
      ((f)localObject1).i(localResultSet.getString("IS_AUTOINCREMENT"));
      localk.c().put(((f)localObject1).b(), localObject1);
    }
    Object localObject1 = paramDatabaseMetaData.getIndexInfo(str3, str2, str1, false, true);
    String str4 = "";
    while (((ResultSet)localObject1).next())
    {
      str4 = ((ResultSet)localObject1).getString("INDEX_NAME");
      localObject2 = ((ResultSet)localObject1).getString("COLUMN_NAME").toUpperCase();
      f localf = (f)localk.c().get(localObject2);
      com.taobao.txc.parser.struct.g localg;
      if (localk.d().containsKey(str4))
      {
        localg = (com.taobao.txc.parser.struct.g)localk.d().get(str4);
        localg.a().add(localf);
      }
      else
      {
        localg = new com.taobao.txc.parser.struct.g();
        localg.b(str4);
        localg.a(((ResultSet)localObject1).getBoolean("NON_UNIQUE"));
        localg.a(((ResultSet)localObject1).getString("INDEX_QUALIFIER"));
        localg.b(((ResultSet)localObject1).getString("INDEX_NAME"));
        localg.a(((ResultSet)localObject1).getShort("TYPE"));
        localg.b(((ResultSet)localObject1).getShort("ORDINAL_POSITION"));
        localg.c(((ResultSet)localObject1).getString("ASC_OR_DESC"));
        localg.a(((ResultSet)localObject1).getInt("CARDINALITY"));
        localg.a().add(localf);
        if ("PRIMARY".equalsIgnoreCase(str4)) {
          localg.a(com.taobao.txc.parser.struct.a.a);
        } else if (!localg.b()) {
          localg.a(com.taobao.txc.parser.struct.a.c);
        } else {
          localg.a(com.taobao.txc.parser.struct.a.b);
        }
        localk.d().put(str4, localg);
      }
    }
    Object localObject2 = (com.taobao.txc.parser.struct.g)localk.d().get(str4);
    if (((com.taobao.txc.parser.struct.g)localObject2).c().a() != 0) {
      if ("H2 JDBC Driver".equals(paramDatabaseMetaData.getDriverName()))
      {
        if ((str4.length() > 11) && ("PRIMARY_KEY".equalsIgnoreCase(str4.substring(0, 11)))) {
          ((com.taobao.txc.parser.struct.g)localObject2).a(com.taobao.txc.parser.struct.a.a);
        }
      }
      else if ((paramDatabaseMetaData.getDriverName() != null) && (paramDatabaseMetaData.getDriverName().toLowerCase().indexOf("postgresql") >= 0) && ((str1 + "_pkey").equalsIgnoreCase(str4))) {
        ((com.taobao.txc.parser.struct.g)localObject2).a(com.taobao.txc.parser.struct.a.a);
      }
    }
    return localk;
  }
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.parser.b.b.a
 * JD-Core Version:    0.7.0.1
 */